package com.mall.service.order.service;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsOrderItem;
import com.mall.model.domain.OmsOrderOperateHistory;
import com.mall.model.dto.DeliveryParamList;
import com.mall.model.dto.MoneyInfoParam;
import com.mall.model.dto.OrderAllDto;
import com.mall.model.dto.ReceiverInfoParam;
import com.mall.model.vo.OrderAllVo;
import com.mall.model.vo.OrderInfo;
import com.mall.model.vo.PageResult;
import com.mall.service.order.mapper.OmsOrderItemMapper;
import com.mall.service.order.mapper.OmsOrderOperateHistoryMapper;
import com.mall.service.order.mapper.OrderMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;


    public void deleteByIds(List<Integer> ids) {
        //判断传过来的是不是空数组,是空的话就直接返回
        orderMapper.deleteBatchIds(ids);

//            OmsOrderItem omsOrderItem = omsOrderItemMapper.selectById(id);
//            String orderSn = omsOrderItem.getOrderSn();
//            orderMapper.deleteById(orderSn);
    }

    public PageResult pageList(Integer pageNum, Integer pageSize, String createTime, String orderSn, Integer orderType, String receiverKeyword, Integer sourceType, Integer status) {
        Page<OmsOrder> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrder> qw = new LambdaQueryWrapper<>();
        qw.eq(orderType != null, OmsOrder::getOrderType, orderType);
        // qw.eq(OmsOrder::getOrderSn,orderSn);
//      qw.like(orderAllDto.getOrderSn()!=null,OmsOrder::getDeliverySn,orderAllDto.getOrderSn());
//      qw.like(orderAllDto.getStatus()!=null,OmsOrder::getStatus,orderAllDto.getStatus());
        qw.eq( status!= null, OmsOrder::getStatus, status);
        qw.eq( sourceType!= null, OmsOrder::getSourceType, sourceType);
      qw.like(!StringUtils.isEmpty(receiverKeyword),OmsOrder::getReceiverName,receiverKeyword);
//      qw.like(orderAllDto.getSourceType()!=null,OmsOrder::getSourceType,orderAllDto.getSourceType());
//      qw.like(orderAllDto.getOrderType()!=null,OmsOrder::getOrderType,orderAllDto.getOrderType());
//qw.like(orderAllDto.getCreateTime()!=null,OmsOrder::getCreateTime,orderAllDto.getCreateTime());
        qw.like(!StringUtils.isEmpty(orderSn), OmsOrder::getOrderSn, orderSn);
//        qw.like(receiverKeyword != null, OmsOrder::getReceiverName, receiverKeyword);
//        qw.in(OmsOrder::getStatus, status);
//        qw.like(receiverKeyword != null, OmsOrder::getBillReceiverPhone, receiverKeyword);
//        qw.in(!StringUtils.isEmpty(orderSn), OmsOrder::getSourceType, sourceType);
//        qw.in(!StringUtils.isEmpty(orderSn), OmsOrder::getOrderType, orderType);
        //    qw.like(createTime!=null,OmsOrder::getCreateTime,createTime);
        Page<OmsOrder> page1 = orderMapper.selectPage(page, qw);
        BeanUtils.copyProperties(page,page1);
        PageResult pageResult = new PageResult();
        pageResult.setList(page1.getRecords());
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotal(Math.toIntExact(page.getTotal()));
        pageResult.setTotalPage(Math.toIntExact(page.getCurrent()));
        return pageResult;


//        IPage<OmsOrder> page1 = orderMapper.selectPage(page, qw);
//        IPage<OrderAllVo>p=new Page<>();
//        BeanUtils.copyProperties(page1,p);

//        IPage<OmsOrder> page1 = orderMapper.selectPage(page, qw);
//        IPage<OrderAllVo>p=new Page<>();
//        BeanUtils.copyProperties(page1,p);
//   OrderAllVo orderAllVo = new OrderAllVo();
//   BeanUtils.copyProperties(omsOrder,orderAllVo);
//   orderAllVo.setPageNum(pageNum);
//   orderAllVo.setPageSize(pageSize);
//   orderAllVo.setTotal(Math.toIntExact(page.getTotal()));
//   orderAllVo.setTotalPage(Math.toIntExact(page.getCurrent()));
//   return orderAllVo;
//        return null;
    }

    public void updateByIds(List<Integer> ids, String note) {
        for (Integer integer : ids) {
            OmsOrder omsOrder = orderMapper.selectById(integer);
            omsOrder.setStatus(4);
            omsOrder.setNote(note);
            orderMapper.updateById(omsOrder);
        }
    }


    public void updateByDe(List<DeliveryParamList> list) {
        List<Integer> orderId = CollUtil.getFieldValues(list, "orderId", Integer.class);
        for (Integer integer : orderId) {
            OmsOrder omsOrder = orderMapper.selectById(integer);
            for (DeliveryParamList deliveryParamList : list) {
                if (deliveryParamList.getOrderId().equals(integer)) {
                    omsOrder.setDeliveryCompany(deliveryParamList.getDeliveryCompany());
                    omsOrder.setDeliverySn(deliveryParamList.getDeliverySn());
                    omsOrder.setStatus(2);
                    orderMapper.updateById(omsOrder);
                }
            }
        }

    }

    public void updateMo(MoneyInfoParam list) {
        //获取订单编号
        Integer orderId = list.getOrderId();
        OmsOrder omsOrder = orderMapper.selectById(orderId);
        omsOrder.setDiscountAmount(list.getDiscountAmount());
        omsOrder.setFreightAmount(list.getFreightAmount());
        omsOrder.setStatus(list.getStatus());
        orderMapper.updateById(omsOrder);
    }

    public void updateNt(Integer id, String note, Integer status) {
        //根据订单号查询订单
        OmsOrder omsOrder = orderMapper.selectById(id);
        omsOrder.setNote(note);
        omsOrder.setStatus(status);
    }

    public void updateInfo(ReceiverInfoParam receiverInfoParam) {
        LambdaQueryWrapper<OmsOrder> qw = new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getOrderSn, receiverInfoParam.getOrderId());
        OmsOrder omsOrder = orderMapper.selectOne(qw);
        BeanUtils.copyProperties(receiverInfoParam, omsOrder);
        orderMapper.updateById(omsOrder);
    }

    public OrderInfo selectInfo(Integer id) {
        LambdaQueryWrapper<OmsOrder> qw = new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getId, id);
        OmsOrder omsOrder = orderMapper.selectOne(qw);
        LambdaQueryWrapper<OmsOrderOperateHistory> q = new LambdaQueryWrapper<>();
        q.eq(OmsOrderOperateHistory::getOrderId, id);
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryMapper.selectList(q);
        LambdaQueryWrapper<OmsOrderItem> w = new LambdaQueryWrapper<>();
        List<OmsOrderItem> omsOrderItems = omsOrderItemMapper.selectList(w);
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(omsOrder, orderInfo);
        orderInfo.setOmsOrderItem(omsOrderItems);
        orderInfo.setOmsOrderOperateHistory(omsOrderOperateHistories);
        return orderInfo;
    }
}
